package net.hn.hnms.biz.safety.fire.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.safety.fire.Service.ODSSensorMonitorService;
import net.hn.hnms.biz.safety.fire.Service.SensorMonitorService;
import net.hn.hnms.biz.safety.fire.entity.ODSSensorMonitor;
import net.hn.hnms.biz.safety.fire.entity.ODSSensorMonitorVo;
import net.hn.hnms.biz.safety.fire.entity.RiskAssessmentVo;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 智呈物联 nzw
 * @date 2022/7/28
 */
@RestController
@RequestMapping("safety/riskWarning/flood/sgjc")
@Api(value = "火灾监测束管监测", tags = {"束管监测"})
public class DWSFireController {

    SensorMonitorService sensorMonitorService;

    ODSSensorMonitorService odsSensorMonitorService;

    private Boolean tableState = null;

    @Autowired
    public void setSensorMonitorService(SensorMonitorService sensorMonitorService) {
        this.sensorMonitorService = sensorMonitorService;
    }
    @Autowired
    public void setOdsSensorMonitorService(ODSSensorMonitorService odsSensorMonitorService) {
        this.odsSensorMonitorService = odsSensorMonitorService;
    }
    /**
     * 束管实时数据
     */
    @GetMapping("")
    @ApiOperation("束管实时数据")
    @AutoSetMineCode
    public R<List<Map<String,Object>>> realTimeList(String mineCode){
        if (tableState==null){
            tableState();
        }
        if (tableState){
            return R.ok(odsSensorMonitorService.latestData(mineCode));
        }else {
            return R.ok(odsSensorMonitorService.latestDataB(mineCode));
        }

    }


    @GetMapping("/history")
    @ApiOperation("束管历史数据")
    @AutoSetMineCode
    public R<List<Map<String,Object>>> List(String SamplingLocation, String state, String end,String mineCode) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date stateTime = null;
        Date endTime = null;
        if (!"".equals(state)&&state!=null){
            stateTime = simpleDateFormat.parse(state);
        }
        if (!"".equals(end)&&end!=null){
            endTime = simpleDateFormat.parse(end);
        }
        return R.ok(odsSensorMonitorService.findAll(SamplingLocation,stateTime,endTime,mineCode));
    }

    @ApiOperation("束管历史分页数据")
    @GetMapping("/history/pageList")
    @AutoSetMineCode
    public R<ResponsePage<ODSSensorMonitorVo>> pageList(PageQuery pageQuery, String state, String end,String mineCode) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date stateTime = null;
        Date endTime = null;
        if (!"".equals(state)&&state!=null){
            stateTime = simpleDateFormat.parse(state);
        }
        if (!"".equals(end)&&end!=null){
            endTime = simpleDateFormat.parse(end);
        }
        return R.ok(odsSensorMonitorService.pageList(pageQuery,stateTime,endTime,mineCode));
    }

    @ApiOperation("导入束管数据")
    @PostMapping("/add")
    public R add(List<Map<String,Object>> list) throws ParseException {

        for (Map<String,Object> map : list){
            ODSSensorMonitor odsSensorMonitor = new ODSSensorMonitor();
            odsSensorMonitor.setId(UUID.randomUUID().toString());

            odsSensorMonitor.setSamplingLocation(map.get("监测点")+"");

            String samplingTime = map.get("监测时间")+"";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            odsSensorMonitor.setSamplingTime(simpleDateFormat.parse(samplingTime));

            odsSensorMonitor.setO2Value(map.get("02")+"");
            odsSensorMonitor.setN2Value(map.get("n2")+"");
            odsSensorMonitor.setCoValue(map.get("co")+"");
            odsSensorMonitor.setCo2Value(map.get("co2")+"");
//            odsSensorMonitor.setch4 //excel表无ch4字段
            odsSensorMonitor.setC2h6Value(map.get("c2h6")+"");
            odsSensorMonitor.setC2h4Value(map.get("c2h4")+"");
            odsSensorMonitor.setC2h2Value(map.get("c2h2")+"");
            odsSensorMonitor.setWxbValue(map.get("烷烯比")+"");

            odsSensorMonitorService.save(odsSensorMonitor);

        }
        return R.ok();
    }

    @ApiOperation("气体曲线")
    @GetMapping("/gasCurve")
    @AutoSetMineCode
    public R<Map<String,Object>> gasCurve(String samplingLocation,String mineCode,String gasType){
        return R.ok(odsSensorMonitorService.gasCurve(samplingLocation,mineCode,gasType));
    }

    @ApiOperation("位置信息")
    @GetMapping("/gasAddress")
    @AutoSetMineCode
    public R<Map<String,Object>> gasAddress(String mineCode){
        return R.ok(odsSensorMonitorService.gasAddress(mineCode));
    }

    /**
     * 一周氧气曲线
     * @return
     */
    @ApiOperation("一周氧气曲线")
    @GetMapping("/oxygenConcentration")
    @AutoSetMineCode
    public R<Map<String,Object>> oxygenConcentration(String samplingLocation,String mineCode){
        return R.ok(odsSensorMonitorService.oxygenConcentration(samplingLocation,mineCode));
    }

    /**
     * 重点气体（ch4,co）曲线
     * @return
     */
    @GetMapping("/keyGas")
    @ApiOperation("重点气体（ch4,co）曲线")
    @AutoSetMineCode
    public R<Map<String,Object>> keyGas(String samplingLocation,String mineCode){
        return R.ok(odsSensorMonitorService.keyGas(samplingLocation,mineCode));
    }

    /**
     * 获取指定地点最新的束管信息
     * @param samplingLocation
     * @return
     */
    @GetMapping("/findByLocation")
    @ApiOperation("获取指定地点最新的束管信息")
    @AutoSetMineCode
    public R<ODSSensorMonitorVo> findByLocation(String samplingLocation, String mineCode) throws ParseException {
        if (tableState==null){
            tableState();
        }
        if (tableState){
            if ("150621011038".equals(mineCode)){
                return R.ok(odsSensorMonitorService.findByLocationGaoTouYao(samplingLocation,mineCode));
            }else {
                return R.ok(odsSensorMonitorService.findByLocation(samplingLocation,mineCode));
            }
        }else {
            if ("150621011038".equals(mineCode)){
                return R.ok(odsSensorMonitorService.findByLocationGaoTouYaoB(samplingLocation,mineCode));
            }else {
                return R.ok(odsSensorMonitorService.findByLocationB(samplingLocation,mineCode));
            }
        }

    }

    /**
     * 获取报警数
     * @return
     */
    @ApiOperation("获取报警数")
    @GetMapping("/alarms")
    @AutoSetMineCode
    public R<Map<String,Object>> alarms(String mineCode){
        return R.ok(odsSensorMonitorService.alarms(mineCode));
    }

    /**
     * 火灾风险评估
     */
    @ApiOperation("火灾风险评估")
    @GetMapping("/riskAssessment")
    public R<RiskAssessmentVo> riskAssessment(){
        RiskAssessmentVo riskAssessmentVo = new RiskAssessmentVo();
        //内因火灾
        riskAssessmentVo.setInternalFire(87.0);
        //外因火灾
        riskAssessmentVo.setExternalFire(95.0);
        //人员得分
        riskAssessmentVo.setPersonnel(93.0);
        //安全管理
        riskAssessmentVo.setSafetyManagement(95.0);
        //综合得分
        riskAssessmentVo.setSafetyManagement(91.8);
        return R.ok(riskAssessmentVo);
    }

    private void tableState(){
        tableState = odsSensorMonitorService.tableState();
    }

    @GetMapping("maxTime")
    @ApiOperation("数据最新时间")
    public R<List<DataStateVo>> getMaxTime(String mineCode, String companyCode){
        return R.ok(odsSensorMonitorService.getMaxTime( mineCode,companyCode));
    }
}
